﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StringService
{
    public class Tools
    {

        /// <summary>
        /// 移除最后一个匹配的字符串。11111111111111111sdfsdfasdfsdf
        /// </summary>
        /// <param name="source"></param>
        /// <param name="lastStr"></param>
        /// <returns></returns>
        public  static string RemoveLastString(string source, string lastStr)
        {
            if (!string.IsNullOrEmpty(source))
            {
                return source.Substring(0, source.LastIndexOf(lastStr));
            }
            return source;
        }

        /// <summary>
        ///把一个字符串数组以指定分隔符合成一个字符串。
        /// </summary>
        /// <param name="arrayss"></param>
        /// <param name="seperator"></param>
        /// <returns></returns>
        public static string ToString(string[] arrayss,string seperator)
        {
            return ToString(arrayss, seperator, "", "");
        }

      
        /// <summary>
        /// 把一个字符串数组以指定分隔符合成一个字符串，并且在合成时可为每个字符加前后添加字符。preStr:前面的字符;endStr:后面的字符。
        /// </summary>
        /// <param name="arrayss"></param>
        /// <param name="seperator"></param>
        /// <param name="preStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string ToString(string[] arrayss, string seperator,string preStr,string endStr)
        {
            StringBuilder b = new StringBuilder();
            foreach (string s in arrayss)
            {
                b.Append(preStr);
                b.Append(s);
                b.Append(endStr);
                b.Append(seperator);
            }
            return RemoveLastString(b.ToString(), seperator);
        }
    }
}
